﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.ContinuousMacro.CUiCMContinuousMacroDlg
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using DnProcessSimulateCommands.ContinuousMacro.Resources;
using EngineeringInternalExtension;
using System;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Input;
using System.Windows.Markup;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using Tecnomatix.Engineering;
using Tecnomatix.Engineering.CommandParameters;
using Tecnomatix.Engineering.Ui;
using Tecnomatix.Engineering.Ui.WPF;


namespace DnProcessSimulateCommands.ContinuousMacro;

internal class CUiCMContinuousMacroDlg : TxWindow, IComponentConnector
{
  private readonly double LowerLimit = 0.0001 / TxApplication.Options.Units.LinearMultiplier;
  private readonly double UpperLimit = 500000.0 / TxApplication.Options.Units.LinearMultiplier;
  private bool IsViewModeChanged;
  private bool IsMacroUndisplayed;
  private bool isOrientViewPointLocked;
  private CApCMContinuousMacroManager macroManager;
  internal TextBox viaLocationName;
  internal TxObjEditBoxControl partPicker;
  internal UniformGrid macroButtonsGrid;
  internal RadioButton noMacro;
  internal Grid unitsGrid;
  internal TextBlock unitType1;
  internal TxNumericEditBoxControl unitValue1;
  internal TextBlock unitName1;
  internal TextBlock unitType2;
  internal TxNumericEditBoxControl unitValue2;
  internal TextBlock unitName2;
  internal CheckBox showPreview;
  internal Image warningIcon;
  internal Button flipPreview;
  internal CheckBox cutThroughCheckbox;
  internal ToggleButton orientViewPoint;
  private bool _contentLoaded;

  public CUiCMContinuousMacroDlg(
    TxRoboticViaLocationOperation viaLocation,
    CApCMMacroShape shape,
    bool isEditing)
  {
    this.InitializeComponent();
    this.partPicker.IgnorPickOfTransientObjects = false;
    this.partPicker.Object = (ITxObject) shape.Part;
    this.partPicker.PickLevel = (TxPickLevel) 1;
    this.partPicker.SetValidator((ITxValidator) new CApCMPartValidator());
    this.InitUntisControls(shape.Shape, shape.Parameters);
    this.viaLocationName.Text = viaLocation.Name;
    this.macroManager = new CApCMContinuousMacroManager(viaLocation, shape);
    this.macroManager.SendFeedback += new Action(this.UpdateControls);
    this.macroManager.IsPreviewVisible = false;
    this.UpdateControls();
    this.SetMode(isEditing);
  }

  private static bool UndisplayMacro(TxRoboticViaLocationOperation viaLocation)
  {
    List<object> objectList = new List<object>();
    List<TxRoboticViaLocationOperation> locationOperationList = new List<TxRoboticViaLocationOperation>();
    objectList.Add((object) locationOperationList);
    TxApplication.CommandsManager.ExecuteCommand("ContinuousProcess.DisplayContinuousMacro", (object) objectList);
    if (!(objectList[0] as List<TxRoboticViaLocationOperation>).Contains(viaLocation))
      return false;
    locationOperationList.Clear();
    locationOperationList.Add(viaLocation);
    objectList.Add((object) false);
    TxApplication.CommandsManager.ExecuteCommand("ContinuousProcess.DisplayContinuousMacro", (object) objectList);
    return true;
  }

  private static void DisplayMacro(TxRoboticViaLocationOperation viaLocation)
  {
    List<object> objectList = new List<object>();
    List<TxRoboticViaLocationOperation> locationOperationList = new List<TxRoboticViaLocationOperation>();
    objectList.Add((object) locationOperationList);
    objectList.Add((object) true);
    locationOperationList.Add(viaLocation);
    TxApplication.CommandsManager.ExecuteCommand("ContinuousProcess.DisplayContinuousMacro", (object) objectList);
  }

  private void SetMode(bool isEditing)
  {
    string str = "pack://application:,,,/DnProcessSimulateCommands;component/ContinuousMacro/Resources/";
    string uriString;
    if (isEditing)
    {
      this.IsMacroUndisplayed = CUiCMContinuousMacroDlg.UndisplayMacro(this.macroManager.ViaLocation);
      // ISSUE: explicit non-virtual call
      __nonvirtual (((Window) this).Title) = ContinuousMacroResources.EDIT_CONTINUOUS_MACRO;
      CUiCMContinuousMacroDlg.PerformActionForRowChildren(this.macroButtonsGrid, 0, new Action<UIElement>(this.SetButtonStateInEditMode));
      uriString = str + "EditContinuousMacro.ico";
    }
    else
    {
      // ISSUE: explicit non-virtual call
      __nonvirtual (((Window) this).Title) = ContinuousMacroResources.CREATE_CONTINUOUS_MACRO;
      this.noMacro.IsEnabled = false;
      uriString = str + "CreateContinuousMacro.ico";
    }
    ((Window) this).Icon = (ImageSource) BitmapFrame.Create(new Uri(uriString, UriKind.RelativeOrAbsolute));
  }

  private void TxWindow_Loaded(object sender, RoutedEventArgs e)
  {
    ((FrameworkElement) this).MinWidth = ((FrameworkElement) this).ActualWidth;
    ((FrameworkElement) this).MinHeight = ((FrameworkElement) this).ActualHeight;
    ((FrameworkElement) this).MaxHeight = ((FrameworkElement) this).ActualHeight;
    ((Window) this).SizeToContent = SizeToContent.Manual;
  }

  private void Root_ValueChanged(object sender, RoutedEventArgs e)
  {
    if (this.macroManager == null)
      return;
    this.macroManager.SetMacroParameters(this.unitValue1.Value, this.unitValue2.Value);
  }

  private void ButtonOK_Click(object sender, RoutedEventArgs e)
  {
    this.macroManager.Save();
    ((Window) this).Close();
  }

  private void ButtonCancel_Click(object sender, RoutedEventArgs e) => ((Window) this).Close();

  private void ButtonOrientViewPoint_Click(object sender, RoutedEventArgs e)
  {
    if (this.orientViewPoint.IsChecked.GetValueOrDefault())
    {
      CApCMContinuousMacroUtilities.OrientViewToLocation(this.macroManager.ViaLocation.AbsoluteLocation, this.macroManager.IsProjectionFlippped);
      if (TxGraphicViewerEx.The.ProjectionType != null)
      {
        TxApplication.CommandsManager.ExecuteCommand("GraphicViewer.ParallelPerspective");
        this.IsViewModeChanged = true;
      }
    }
    this.macroManager.ViaLocation.AbsoluteLocationChanged -= new TxLocatableObject_AbsoluteLocationChangedEventHandler(this.ViaLocation_AbsoluteLocationChanged);
    this.orientViewPoint.IsChecked = new bool?(this.isOrientViewPointLocked);
    if (!this.isOrientViewPointLocked)
      return;
    this.macroManager.ViaLocation.AbsoluteLocationChanged += new TxLocatableObject_AbsoluteLocationChangedEventHandler(this.ViaLocation_AbsoluteLocationChanged);
    this.isOrientViewPointLocked = false;
  }

  private void ButtonOrientViewPoint_DuobleClick(object sender, RoutedEventArgs e)
  {
    this.isOrientViewPointLocked = true;
  }

  private void ViaLocation_AbsoluteLocationChanged(
    object sender,
    TxLocatableObject_AbsoluteLocationChangedEventArgs args)
  {
    CApCMContinuousMacroUtilities.OrientViewToLocation(this.macroManager.ViaLocation.AbsoluteLocation, this.macroManager.IsProjectionFlippped);
  }

  private void ButtonPlacement_Click(object sender, RoutedEventArgs e)
  {
    TxObjectList txObjectList = new TxObjectList(1);
    ((Collection<ITxObject>) txObjectList).Add((ITxObject) this.macroManager.ViaLocation);
    TxApplication.CommandsManager.ExecuteCommand("Placement.PlacementManipulator", (object) new TxMultipleObjectParameters(txObjectList));
  }

  private void ButtonFlipPreview_Click(object sender, RoutedEventArgs e)
  {
    this.macroManager.IsProjectionFlippped = !this.macroManager.IsProjectionFlippped;
    if (this.orientViewPoint.IsChecked.GetValueOrDefault())
      CApCMContinuousMacroUtilities.OrientViewToLocation(this.macroManager.ViaLocation.AbsoluteLocation, this.macroManager.IsProjectionFlippped);
    TxApplication.RefreshDisplay();
  }

  private void ButtonFlip_Click(object sender, RoutedEventArgs e)
  {
    this.macroManager.ViaLocation.AbsoluteLocation = CApCMContinuousMacroUtilities.FlipZAxis(this.macroManager.ViaLocation.AbsoluteLocation);
    TxApplication.RefreshDisplay();
  }

  private void RadioButton_Checked(object sender, RoutedEventArgs e)
  {
    if ((sender as RadioButton).Tag == null || this.unitValue1.Value <= 0.0 || this.unitValue2.Value <= 0.0)
      return;
    CApCMShape tag = (CApCMShape) (sender as RadioButton).Tag;
    this.SetUntisControlsVisibility(tag);
    if (this.macroManager == null)
      return;
    this.macroManager.SetMacro(tag, this.unitValue1.Value, this.unitValue2.Value);
  }

  private void InitUntisControls(CApCMShape shapeType, double[] macroParameters)
  {
    this.unitValue1.LowerBound = this.LowerLimit;
    this.unitValue2.LowerBound = this.LowerLimit;
    int length = macroParameters.Length;
    this.unitValue1.Value = length > 0 ? macroParameters[0] : 100.0;
    this.unitValue2.Value = length > 1 ? macroParameters[1] : 100.0;
    this.SetUntisControlsVisibility(shapeType);
    this.unitName1.Text = $"({TxApplication.Options.Units.LinearUnitName})";
    this.unitName2.Text = $"({TxApplication.Options.Units.LinearUnitName})";
  }

  private void SetUntisControlsVisibility(CApCMShape shape)
  {
    switch (shape)
    {
      case CApCMShape.None:
        CUiCMContinuousMacroDlg.PerformActionForRowChildren(this.unitsGrid, 0, (Action<UIElement>) (x => x.Visibility = Visibility.Hidden));
        CUiCMContinuousMacroDlg.PerformActionForRowChildren(this.unitsGrid, 1, (Action<UIElement>) (x => x.Visibility = Visibility.Hidden));
        break;
      case CApCMShape.Circle:
        CUiCMContinuousMacroDlg.PerformActionForRowChildren(this.unitsGrid, 0, (Action<UIElement>) (x => x.Visibility = Visibility.Visible));
        CUiCMContinuousMacroDlg.PerformActionForRowChildren(this.unitsGrid, 1, (Action<UIElement>) (x => x.Visibility = Visibility.Hidden));
        this.unitValue1.UpperBound = this.UpperLimit / 2.0;
        this.unitType1.Text = ContinuousMacroResources.RADIUS;
        break;
      case CApCMShape.Slot:
        CUiCMContinuousMacroDlg.PerformActionForRowChildren(this.unitsGrid, 0, (Action<UIElement>) (x => x.Visibility = Visibility.Visible));
        CUiCMContinuousMacroDlg.PerformActionForRowChildren(this.unitsGrid, 1, (Action<UIElement>) (x => x.Visibility = Visibility.Visible));
        this.unitValue1.UpperBound = this.UpperLimit;
        this.unitValue2.UpperBound = this.UpperLimit / 2.0;
        this.unitType1.Text = ContinuousMacroResources.LENGTH;
        this.unitType2.Text = ContinuousMacroResources.RADIUS;
        break;
      case CApCMShape.Rectangle:
        CUiCMContinuousMacroDlg.PerformActionForRowChildren(this.unitsGrid, 0, (Action<UIElement>) (x => x.Visibility = Visibility.Visible));
        CUiCMContinuousMacroDlg.PerformActionForRowChildren(this.unitsGrid, 1, (Action<UIElement>) (x => x.Visibility = Visibility.Visible));
        this.unitValue1.UpperBound = this.UpperLimit;
        this.unitValue2.UpperBound = this.UpperLimit;
        this.unitType1.Text = ContinuousMacroResources.LENGTH;
        this.unitType2.Text = ContinuousMacroResources.WIDTH;
        break;
    }
  }

  private static void PerformActionForRowChildren(Grid grid, int row, Action<UIElement> action)
  {
    foreach (UIElement uiElement in grid.Children.Cast<UIElement>().Where<UIElement>((Func<UIElement, bool>) (x => Grid.GetRow(x) == row)))
      action(uiElement);
  }

  private static void PerformActionForRowChildren(
    UniformGrid grid,
    int row,
    Action<UIElement> action)
  {
    foreach (UIElement uiElement in grid.Children.Cast<UIElement>().Where<UIElement>((Func<UIElement, bool>) (x => Grid.GetRow(x) == row)))
      action(uiElement);
  }

  private void ShowPreviewOnPartChanged(object sender, RoutedEventArgs e)
  {
    if (this.macroManager == null)
      return;
    bool valueOrDefault = this.showPreview.IsChecked.GetValueOrDefault();
    this.macroManager.IsPreviewVisible = valueOrDefault;
    this.cutThroughCheckbox.IsEnabled = valueOrDefault;
    this.flipPreview.IsEnabled = valueOrDefault;
  }

  private void SetButtonStateInEditMode(UIElement button)
  {
    RadioButton radioButton = button as RadioButton;
    CApCMShape tag = (CApCMShape) radioButton.Tag;
    radioButton.IsEnabled = tag == CApCMShape.None || tag == this.macroManager.Shape;
    if (!radioButton.IsEnabled)
      (radioButton.Content as Canvas).Opacity = 0.3;
    radioButton.IsChecked = new bool?(tag == this.macroManager.Shape);
  }

  private void TxWindow_Closed(object sender, EventArgs e)
  {
    TxRoboticViaLocationOperation viaLocation = this.macroManager.ViaLocation;
    viaLocation.AbsoluteLocationChanged -= new TxLocatableObject_AbsoluteLocationChangedEventHandler(this.ViaLocation_AbsoluteLocationChanged);
    this.macroManager.Delete();
    if (this.IsMacroUndisplayed)
      CUiCMContinuousMacroDlg.DisplayMacro(viaLocation);
    if (this.IsViewModeChanged)
      TxApplication.CommandsManager.ExecuteCommand("GraphicViewer.ParallelPerspective");
    else
      TxApplication.RefreshDisplay();
  }

  private void partPicker_TypeInvalid(object sender, EventArgs e)
  {
    this.macroManager.Part = (ITxLocatableObject) null;
  }

  private void partPicker_TypeValid(object sender, EventArgs e)
  {
    this.macroManager.Part = this.partPicker.Object as ITxLocatableObject;
  }

  private void partPicker_Picked(object sender, TxObjEditBoxCtrl_PickedEventArgs args)
  {
    this.macroManager.Part = this.partPicker.Object as ITxLocatableObject;
  }

  private void UpdateControls()
  {
    switch (this.macroManager.ProjectionWarning)
    {
      case ProjectionWarning.NoPart:
        this.SetWarnigIcon(ContinuousMacroResources.NO_SELECTED_PART_WARNING);
        break;
      case ProjectionWarning.NoProjection:
        this.SetWarnigIcon(ContinuousMacroResources.NO_PROJECTION_WARNING);
        break;
      default:
        this.warningIcon.Visibility = Visibility.Hidden;
        break;
    }
  }

  private void SetWarnigIcon(string tooltipText)
  {
    this.warningIcon.Visibility = Visibility.Visible;
    ToolTip toolTip = new ToolTip();
    toolTip.Content = (object) tooltipText;
    this.warningIcon.ToolTip = (object) toolTip;
  }

  private void ShowPreviewAsCutThroughChanged(object sender, RoutedEventArgs e)
  {
    if (this.macroManager == null)
      return;
    this.macroManager.IsPreviewCutThrough = ((int) (sender as CheckBox).IsChecked ?? 1) != 0;
  }

  [DebuggerNonUserCode]
  [GeneratedCode("PresentationBuildTasks", "4.0.0.0")]
  public void InitializeComponent()
  {
    if (this._contentLoaded)
      return;
    this._contentLoaded = true;
    Application.LoadComponent((object) this, new Uri("/DnProcessSimulateCommands;component/continuousmacro/createandedit/cuicmcontinuousmacrodlg.xaml", UriKind.Relative));
  }

  [DebuggerNonUserCode]
  [GeneratedCode("PresentationBuildTasks", "4.0.0.0")]
  [EditorBrowsable(EditorBrowsableState.Never)]
  void IComponentConnector.Connect(int connectionId, object target)
  {
    switch (connectionId)
    {
      case 1:
        ((FrameworkElement) target).Loaded += new RoutedEventHandler(this.TxWindow_Loaded);
        ((Window) target).Closed += new EventHandler(this.TxWindow_Closed);
        break;
      case 2:
        this.viaLocationName = (TextBox) target;
        break;
      case 3:
        this.partPicker = (TxObjEditBoxControl) target;
        this.partPicker.TypeInvalid += new EventHandler(this.partPicker_TypeInvalid);
        this.partPicker.TypeValid += new EventHandler(this.partPicker_TypeValid);
        this.partPicker.Picked += new TxObjEditBoxCtrl_PickedEventHandler(this.partPicker_Picked);
        break;
      case 4:
        ((ButtonBase) target).Click += new RoutedEventHandler(this.ButtonPlacement_Click);
        break;
      case 5:
        ((ButtonBase) target).Click += new RoutedEventHandler(this.ButtonFlip_Click);
        break;
      case 6:
        this.macroButtonsGrid = (UniformGrid) target;
        break;
      case 7:
        ((ToggleButton) target).Checked += new RoutedEventHandler(this.RadioButton_Checked);
        break;
      case 8:
        ((ToggleButton) target).Checked += new RoutedEventHandler(this.RadioButton_Checked);
        break;
      case 9:
        ((ToggleButton) target).Checked += new RoutedEventHandler(this.RadioButton_Checked);
        break;
      case 10:
        this.noMacro = (RadioButton) target;
        this.noMacro.Checked += new RoutedEventHandler(this.RadioButton_Checked);
        break;
      case 11:
        this.unitsGrid = (Grid) target;
        break;
      case 12:
        this.unitType1 = (TextBlock) target;
        break;
      case 13:
        this.unitValue1 = (TxNumericEditBoxControl) target;
        this.unitValue1.ValueChanged += new RoutedEventHandler(this.Root_ValueChanged);
        break;
      case 14:
        this.unitName1 = (TextBlock) target;
        break;
      case 15:
        this.unitType2 = (TextBlock) target;
        break;
      case 16 /*0x10*/:
        this.unitValue2 = (TxNumericEditBoxControl) target;
        this.unitValue2.ValueChanged += new RoutedEventHandler(this.Root_ValueChanged);
        break;
      case 17:
        this.unitName2 = (TextBlock) target;
        break;
      case 18:
        this.showPreview = (CheckBox) target;
        this.showPreview.Checked += new RoutedEventHandler(this.ShowPreviewOnPartChanged);
        this.showPreview.Unchecked += new RoutedEventHandler(this.ShowPreviewOnPartChanged);
        break;
      case 19:
        this.warningIcon = (Image) target;
        break;
      case 20:
        this.flipPreview = (Button) target;
        this.flipPreview.Click += new RoutedEventHandler(this.ButtonFlipPreview_Click);
        break;
      case 21:
        this.cutThroughCheckbox = (CheckBox) target;
        this.cutThroughCheckbox.Unchecked += new RoutedEventHandler(this.ShowPreviewAsCutThroughChanged);
        this.cutThroughCheckbox.Checked += new RoutedEventHandler(this.ShowPreviewAsCutThroughChanged);
        break;
      case 22:
        this.orientViewPoint = (ToggleButton) target;
        this.orientViewPoint.Click += new RoutedEventHandler(this.ButtonOrientViewPoint_Click);
        this.orientViewPoint.MouseDoubleClick += new MouseButtonEventHandler(this.ButtonOrientViewPoint_DuobleClick);
        break;
      case 23:
        ((ButtonBase) target).Click += new RoutedEventHandler(this.ButtonOK_Click);
        break;
      case 24:
        ((ButtonBase) target).Click += new RoutedEventHandler(this.ButtonCancel_Click);
        break;
      default:
        this._contentLoaded = true;
        break;
    }
  }
}
